-- 衍生 募兵
local ref_mubing = fk.CreateSkill {
    name = "mcdh_ref_mubing",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

Fk:addPoxiMethod{
    name = "mcdh_ref_mubing_prey",
    card_filter = function(to_select, selected)
        return true
    end,
    feasible = function(selected, data)
        if not data or #selected == 0 then return false end
        local count = 0
        for _, id in ipairs(selected) do
            local num = Fk:getCardById(id).number
            if table.contains(data[1][2], id) then
                count = count - num
            else
                count = count + num
            end
        end
        return count >= 0
    end,
    prompt = function ()
        return Fk:translate("#mcdh_ref_mubing-prompt")
    end
}

ref_mubing:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self) and player.phase == Player.Play
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cards = room:getNCards(3)
        room:moveCards({
            ids = cards,
            toArea = Card.Processing,
            moveReason = fk.ReasonPut,
            skillName = self.name,
            proposer = player.id
        })
        local can_throw = table.filter(player:getCardIds("h"), function(id) return not player:prohibitDiscard(Fk:getCardById(id)) end)
        if #can_throw > 0 then
            local result = room:askForPoxi(player, "mcdh_ref_mubing_prey", {
                { "mcdh_ref_mubing_to_prey", cards },
                { "mcdh_ref_mubing_to_throw", can_throw },
            }, nil, true)
            local get, throw = {}, {}
            for _, id in ipairs(result) do
                if table.contains(cards, id) then
                    table.insert(get, id)
                else
                    table.insert(throw, id)
                end
            end
            if #throw > 0 then
                room:throwCard(throw, self.name, player, player)
            end
            if #get > 0 and not player.dead then
                local dummy = Fk:cloneCard("dilu")
                dummy:addSubcards(get)
                room:obtainCard(player.id, dummy, true, fk.ReasonPrey)
            end
        end
        cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
        if #cards > 0 then
            room:moveCards({
                ids = cards,
                toArea = Card.DiscardPile,
                moveReason = fk.ReasonPutIntoDiscardPile,
            })
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_ref_mubing"] = "募兵",
    [":mcdh_ref_mubing"] = "出牌阶段开始时，你可以亮出牌堆顶三张牌，然后你可以弃置任意张手牌，获得任意张亮出的牌，你弃置牌点数之和不能小于获得牌点数之和。",
    ["mcdh_ref_mubing_prey"] = "募兵",
    ["#mcdh_ref_mubing-prompt"] = "募兵：选择你要获得和弃置的牌",
    ["mcdh_ref_mubing_to_prey"] = "获得",
    ["mcdh_ref_mubing_to_throw"] = "弃置",
}


return ref_mubing